<?php

namespace App\Admin\Controllers\Agent;

use App\Admin\Widgets\InfoBox;
use App\Http\Controllers\Controller;
use App\Models\AgentInfo;
use App\Models\Order;
use App\Models\OrderReadInfo;
use Carbon\Carbon;
use Encore\Admin\Layout\Column;
use Encore\Admin\Layout\Content;
use Encore\Admin\Layout\Row;
use Encore\Admin\Widgets\Tab;
use Encore\Admin\Widgets\Table;
use Illuminate\Support\Facades\Auth;

class HomeController extends Controller
{
    public function index(Content $content)
    {
        return $content
            ->title('首页')
            ->description(Carbon::now()->toDateTimeString())
            ->row(function (Row $row) {

                $row->column(3, function (Column $column) {

                    $today_amount = OrderReadInfo::query()->whereHas('order', function ($order) {
                        $order->where('admin_user_id', Auth::guard()->id());
                    })->whereDate('date', Carbon::now()->toDateString())->sum('amount');

                    $card = new InfoBox('今日消耗', '', 'red', null, $today_amount);
                    $column->append($card);
                });
                $row->column(3, function (Column $column) {

                    $yesterday_amount = OrderReadInfo::query()->whereHas('order', function ($order) {
                        $order->where('admin_user_id', Auth::guard()->id());
                    })->whereDate('date',  Carbon::now()->subDays(1)->toDateString())->sum('amount');
                    $card = new InfoBox('昨日消耗', '', 'green', null, $yesterday_amount);
                    $column->append($card);
                });

                $row->column(3, function (Column $column) {

                    $amount = AgentInfo::query()->where('admin_user_id',Auth::guard()->id())->first()->balance;

                    $card = new InfoBox('账户余额', '', 'orange', null, $amount);
                    $column->append($card);
                });


                $row->column(3, function (Column $column) {
                    $order_on_num = Order::query()->where('admin_user_id', Auth::guard()->id())->where('state', Order::ORDER_STATE_ON)->count();
                    $card = new InfoBox('在投订单', '', 'yellow', null, $order_on_num);
                    $column->append($card);
                });
            })
            ->row(function (Row $row) {

//                //取七天内的数据
                $row->column(12, function (Column $column) {
                    $start_date = Carbon::now()->subDays(7)->toDateString();
                    $end_date = Carbon::now()->toDateString();
                    $rows = OrderReadInfo::query()->groupBy('date')->whereBetween('date',[$start_date,$end_date])
                        ->whereHas('order',function($order){
                            $order->where('admin_user_id',Auth::guard()->id());
                        })->orderByDesc('date')
                        ->select('date')
                        ->selectRaw('sum(amount) as amount')
                        ->selectRaw('count(distinct order_id) as order_num')
                        ->get()->toArray();
                    $headers = ['日期','消耗金额','在投订单'];
                    $table = new Table($headers, $rows);
                    $tab = new Tab();
                    $tab->add('七日数据', $table);
                    $column->append($tab);
                });

            });
    }
}
